﻿using Stote.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Stote.Forms
{
    public partial class FrmInventory : Form
    {
        Dong.BLL.StoreArea storeAreaBLL = new Dong.BLL.StoreArea();
        Dong.BLL.Inventory inventoryBLL = new Dong.BLL.Inventory();

        #region 构造函数
        public FrmInventory()
        {
            InitializeComponent();
            Bind();

            btnSearch.Click += btnSearch_Click;
            ucPager1.PageChanged += UcPager1_PageChanged;
            dataGridView1.CellFormatting += dataGridView1_CellFormatting;
        }
        #endregion

        #region 数据绑定
        private void Bind()
        {
            DataSet ds = storeAreaBLL.GetList(" Status=0");
            DataTable dt = ds.Tables[0];
            DataRow dr = dt.NewRow();
            dr["StoreAreaId"] = -1;
            dr["StoreAreaName"] = "全部";
            dt.Rows.InsertAt(dr, 0);
            cbStoreArea.DataSource = dt;
            cbStoreArea.DisplayMember = "StoreAreaName";
            cbStoreArea.ValueMember = "StoreAreaId";

            List<Dong.Model.Status> list = new List<Dong.Model.Status>() {
                new Dong.Model.Status(){Id=-1,Name="全部"},
                new Dong.Model.Status(){Id=0,Name="正常"},
                new Dong.Model.Status(){Id=1,Name="禁用"},
            };
            cbStatus.DataSource = list;
            cbStatus.DisplayMember = "Name";
            cbStatus.ValueMember = "Id";
        }
        #endregion

        #region 打开添加窗体
        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmStorageRecordAddAndEdit form = new FrmStorageRecordAddAndEdit(0, this);
            if (form.ShowDialog() == DialogResult.OK)
            {
                btnSearch.PerformClick();
            }
        }
        #endregion

        #region 查询及数据绑定
        private void UcPager1_PageChanged(int pageSize, int currentPage, out int totalCount)
        {
            int startIndex = (currentPage - 1) * pageSize + 1;
            int endIndex = startIndex + pageSize - 1;
            string strWhere = $" 1=1";
            if (!string.IsNullOrEmpty(txtProductName.Text.Trim()))
                strWhere += $" AND ProductName LIKE '%{txtProductName.Text.Trim()}%'";
            if (Convert.ToInt32(cbStoreArea.SelectedValue) != -1)
                strWhere += $" AND StoreAreaId={Convert.ToInt32(cbStoreArea.SelectedValue)}";
            if (Convert.ToInt32(cbStatus.SelectedValue) != -1)
                strWhere += $" AND Status={Convert.ToInt32(cbStatus.SelectedValue)}";

            DataSet ds = inventoryBLL.GetListByPage(strWhere, "CreateTime DESC", startIndex, endIndex);

            totalCount = inventoryBLL.GetRecordCount(strWhere);
            dataGridView1.DataSource = ds.Tables[0];

            DataGridViewHelper.DataGridStyle(dataGridView1);

            dataGridView1.Columns["Row"].Visible = false;
            dataGridView1.Columns["StoreId"].Visible = false;
            dataGridView1.Columns["StoreAreaId"].Visible = false;
            dataGridView1.Columns["ProductId"].Visible = false;
            dataGridView1.Columns["Remark"].Visible = false;
            dataGridView1.Columns["Status"].Visible = false;
            dataGridView1.Columns["CreateUserId"].Visible = false;
            dataGridView1.Columns["LastUpdateUserId"].Visible = false;
            dataGridView1.Columns["LastUpdateUserName"].Visible = false;
            dataGridView1.Columns["LastUpdateTime"].Visible = false;

            dataGridView1.Columns["InventoryId"].HeaderText = "主键";
            dataGridView1.Columns["StoreId"].HeaderText = "仓库Id";
            dataGridView1.Columns["StoreName"].HeaderText = "仓库名称";
            dataGridView1.Columns["StoreAreaId"].HeaderText = "区域Id";
            dataGridView1.Columns["StoreAreaName"].HeaderText = "区域名称";
            dataGridView1.Columns["ProductId"].HeaderText = "产品Id";
            dataGridView1.Columns["ProductName"].HeaderText = "产品名称";
            dataGridView1.Columns["Amount"].HeaderText = "产品数量";
            dataGridView1.Columns["Remark"].HeaderText = "备注";
            dataGridView1.Columns["Status"].HeaderText = "状态";
            dataGridView1.Columns["StatusName"].HeaderText = "状态";
            dataGridView1.Columns["CreateUserId"].HeaderText = "创建人Id";
            dataGridView1.Columns["CreateUserName"].HeaderText = "创建人";
            dataGridView1.Columns["CreateTime"].HeaderText = "创建时间";
            dataGridView1.Columns["LastUpdateUserId"].HeaderText = "修改人Id";
            dataGridView1.Columns["LastUpdateUserName"].HeaderText = "修改人";
            dataGridView1.Columns["LastUpdateTime"].HeaderText = "修改时间";

            dataGridView1.Columns["CreateTime"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            dataGridView1.Columns["LastUpdateTime"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";

            dataGridView1.Columns["InventoryId"].Width = 70;
            dataGridView1.Columns["Amount"].Width = 100;
            dataGridView1.Columns["CreateUserName"].Width = 90;
            dataGridView1.Columns["CreateTime"].Width = 220;
            dataGridView1.Columns["LastUpdateUserName"].Width = 90;
            dataGridView1.Columns["LastUpdateTime"].Width = 220;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            ucPager1.CurrentPage = 1;
            ucPager1.TriggerPageChanged();
        }
        #endregion

        #region 网格列格式化
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("StatusName"))
            {
                if (e.Value.ToString() == "正常")
                {
                    e.CellStyle.ForeColor = Color.White;
                }
                else
                {
                    e.CellStyle.ForeColor = Color.Red;
                }
            }
        }
        #endregion
    }
}
